﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using QuanLyVanBanBUS;

public partial class QLVB_Admin_CaiDatThamSoNguoiDung : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            layDanhSachLoaiNguoiDung();
            
            layDanhSachChucVu();

            layDanhSachPhong();
        }
    }

    //*****Table LoaiNguoiDung*****
    protected void layDanhSachLoaiNguoiDung()
    {
        LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();
        gvLoaiNguoiDung.DataSource = lndBUS.layDanhSachLoaiNguoiDung();
        gvLoaiNguoiDung.DataBind();
    }
    protected void btnThemLoaiNguoiDung_Click(object sender, EventArgs e)
    {
        LoaiNguoiDung lndDTO = new LoaiNguoiDung();
        lndDTO.MaLoaiNguoiDung = txtMaLoaiNguoiDung.Text.ToUpper();
        lndDTO.TenLoaiNguoiDung = txtTenLoaiNguoiDung.Text.ToUpper();

        //Kiem tra rong
        if (lndDTO.MaLoaiNguoiDung.ToString() == string.Empty)
        {
            Alert.Show("Ma loai nguoi dung khong duoc rong");
            return;
        }

        if (lndDTO.TenLoaiNguoiDung.ToString() == string.Empty)
        {
            Alert.Show("Ten loai nguoi dung khong duoc rong");
            return;
        }

        //Them
        if (lndDTO.MaLoaiNguoiDung.ToString() != string.Empty && lndDTO.TenLoaiNguoiDung.ToString() != string.Empty)
        {
            LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();

            if (lndBUS.kiemtraTonTaiMaLoaiNguoiDung(lndDTO.MaLoaiNguoiDung.ToUpper().ToString()) == false)
            {
                if (lndBUS.kiemtraTonTaiTenLoaiNguoiDung(lndDTO.TenLoaiNguoiDung.ToUpper().ToString()) == false)
                {
                    lndBUS.themLoaiNguoiDung(lndDTO);

                    layDanhSachLoaiNguoiDung();

                    MyTextBox myTextBox = new MyTextBox();
                    myTextBox.Xoa(txtMaLoaiNguoiDung);
                    myTextBox.Xoa(txtTenLoaiNguoiDung);
                }
                else
                {
                    Alert.Show("Ten loai nguoi dung nay da ton tai");
                    return;
                }
            }
            else
            {
                Alert.Show("Ma loai nguoi dung nay da ton tai");
                return;
            }
        }
    }
    protected void gvLoaiNguoiDung_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvLoaiNguoiDung.PageIndex = e.NewPageIndex;

        layDanhSachLoaiNguoiDung();
    }
    protected void btnXoaLoaiNguoiDung_Click(object sender, EventArgs e)
    {
        LoaiNguoiDungBUS lvbBUS = new LoaiNguoiDungBUS();

        lvbBUS.xoaLoaiNguoiDung(gvLoaiNguoiDung);
    }
    //*****

    //*****Table ChucVu*****
    protected void layDanhSachChucVu()
    {
        ChucVuBUS cvBUS = new ChucVuBUS();
        gvChucVu.DataSource = cvBUS.layDanhSachChucVu();
        gvChucVu.DataBind();
    }
    protected void btnThemChucVu_Click(object sender, EventArgs e)
    {
        ChucVu cvDTO = new ChucVu();
        cvDTO.MaChucVu = txtMaChucVu.Text.ToUpper();
        cvDTO.TenChucVu = txtTenChucVu.Text.ToUpper();

        //Kiem tra rong
        if (cvDTO.MaChucVu.ToString() == string.Empty)
        {
            Alert.Show("Ma chuc vu khong duoc rong");
            return;
        }

        if (cvDTO.TenChucVu.ToString() == string.Empty)
        {
            Alert.Show("Ten chuc vu khong duoc rong");
            return;
        }

        //Them
        if (cvDTO.MaChucVu.ToString() != string.Empty && cvDTO.TenChucVu.ToString() != string.Empty)
        {
            ChucVuBUS cvBUS = new ChucVuBUS();

            if (cvBUS.kiemtraTonTaiMaChucVu(cvDTO.MaChucVu.ToUpper().ToString()) == false)
            {
                if (cvBUS.kiemtraTonTaiTenChucVu(cvDTO.TenChucVu.ToUpper().ToString()) == false)
                {
                    cvBUS.themChucVu(cvDTO);

                    layDanhSachChucVu();

                    MyTextBox myTextBox = new MyTextBox();
                    myTextBox.Xoa(txtMaChucVu);
                    myTextBox.Xoa(txtTenChucVu);
                }
                else
                {
                    Alert.Show("Ten chuc vu nay da ton tai");
                    return;
                }
            }
            else
            {
                Alert.Show("Ma chuc vu nay da ton tai");
                return;
            }
        }
    }
    protected void gvChucVu_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvChucVu.PageIndex = e.NewPageIndex;

        layDanhSachChucVu();
    }
    protected void btnXoaChucVu_Click(object sender, EventArgs e)
    {
        ChucVuBUS cvBUS = new ChucVuBUS();

        cvBUS.xoaChucVu(gvChucVu);
    }
    //*****

    //*****Table Phong*****
    protected void layDanhSachPhong()
    {
        PhongBUS pBUS = new PhongBUS();
        gvPhong.DataSource = pBUS.layDanhSachPhong();
        gvPhong.DataBind();
    }
    protected void btnThemPhong_Click(object sender, EventArgs e)
    {
        Phong pDTO = new Phong();
        pDTO.MaPhong = txtMaPhong.Text.ToUpper();
        pDTO.TenPhong = txtTenPhong.Text.ToUpper();
        pDTO.DienThoaiNoiBo = txtDienThoaiNoiBo.Text;
        pDTO.DienThoaiNgoai = txtDienThoaiNgoai.Text;
        pDTO.Fax = txtFax.Text;

        //Kiem tra rong
        if (pDTO.MaPhong.ToString() == string.Empty)
        {
            Alert.Show("Ma phong khong duoc rong");
            return;
        }

        if (pDTO.TenPhong.ToString() == string.Empty)
        {
            Alert.Show("Ten phong khong duoc rong");
            return;
        }

        if (pDTO.DienThoaiNoiBo.ToString() == string.Empty)
        {
            Alert.Show("Dien thoai noi bo khong duoc rong");
            return;
        }

        if (pDTO.DienThoaiNgoai.ToString() == string.Empty)
        {
            Alert.Show("Dien thoai ngoai khong duoc rong");
            return;
        }

        if (pDTO.Fax.ToString() == string.Empty)
        {
            Alert.Show("So fax khong duoc rong");
            return;
        }

        //Them
        if (pDTO.MaPhong.ToString() != string.Empty && pDTO.TenPhong.ToString() != string.Empty && pDTO.DienThoaiNoiBo.ToString() != string.Empty && pDTO.DienThoaiNgoai.ToString() != string.Empty && pDTO.Fax.ToString() != string.Empty)
        {
            PhongBUS pBUS = new PhongBUS();

            if (pBUS.kiemtraTonTaiMaPhong(pDTO.MaPhong.ToUpper().ToString()) == false)
            {
                if (pBUS.kiemtraTonTaiTenPhong(pDTO.TenPhong.ToUpper().ToString()) == false)
                {
                    pBUS.themPhong(pDTO);

                    layDanhSachPhong();

                    MyTextBox myTextBox = new MyTextBox();
                    myTextBox.Xoa(txtMaPhong);
                    myTextBox.Xoa(txtTenPhong);
                }
                else
                {
                    Alert.Show("Ten phong nay da ton tai");
                    return;
                }
            }
            else
            {
                Alert.Show("Ma phong nay da ton tai");
                return;
            }
        }
    }
    protected void gvPhong_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvPhong.PageIndex = e.NewPageIndex;

        layDanhSachPhong();
    }
    protected void btnXoaPhong_Click(object sender, EventArgs e)
    {
        PhongBUS pBUS = new PhongBUS();

        pBUS.xoaPhong(gvPhong);
    }
    //*****
}